package gui.toolbox;

import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Statebar extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = -4347314820725364095L;
	private final JTextField textField = new JTextField();
	private final JButton copyButton = new JButton("Copy");
	private final BorderLayout layout = new BorderLayout();

	/**
	 * Build an new statebar with copy button
	 */
	public Statebar() {
		super();
		textField.setEditable(false);
		textField.setColumns(30);
		this.setLayout(layout);
		this.add(textField, BorderLayout.WEST);
		this.add(copyButton, BorderLayout.EAST);
		copyButton.setActionCommand("Pressed");
		copyButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (e.getActionCommand().equals("Pressed")) {
					Clipboard clip = Toolkit.getDefaultToolkit()
							.getSystemClipboard();
					clip.setContents(new StringSelection(textField.getText()),
							new ClipboardOwner() {
								@Override
								public void lostOwnership(Clipboard clipboard,
										Transferable contents) {

								}
							});
				}
			}
		});
	}

	/**
	 * Set text inside the statebar (can be copyed to the clipboard after)
	 * 
	 * @param text
	 *            the text to be added
	 */
	public void setText(String text) {
		textField.setText(text);
	}
}
